package h0907;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
  * @description 字母异位词分组
  * @author 不知名帅哥
  * @date 2024/9/7 21:33
  * @version 1.0
*/
public class GroupingOfLetterOdorWords {
    public static void main(String[] args) {

    }
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        int n = strs.length;
        for (int i = 0; i < n; i++) {
            String str = strs[i];
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String temp=new String(chars);
            if (map.containsKey(temp)){
                List<String> list = map.get(temp);
                list.add(str);
                map.put(temp,list);
            }else {
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(temp,list);
            }
        }
        return map.values().stream().toList();
    }
}
